package humanResource.staffManagement;

import entity.AccessRole;
import entity.StaffAccount;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class RoleManagementSession implements RoleManagementSessionRemote {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public long createRole(String roleName, Set<String> access) {
        if (roleName == null) {
            return 0;
        }

        AccessRole ar = new AccessRole();
        ar.setRoleName(roleName);
        ar.setAccess(access);

        entityManager.persist(ar);
        entityManager.flush();

        return ar.getId();
    }

    @Override
    public List<AccessRole> getAllRoles() {
        String strQuery = "SELECT r FROM AccessRole r";
        Query query = entityManager.createQuery(strQuery);
        return query.getResultList();
    }

    @Override
    public boolean editRole(long roleID, String roleName, Set<String> access) {
        AccessRole ar = entityManager.find(AccessRole.class, roleID);

        if (ar == null) {
            return false;
        }

        ar.setRoleName(roleName);
        ar.setAccess(access);

        entityManager.persist(ar);
        entityManager.flush();
        return true;
    }

    @Override
    public Set<String> getRoleAccess(long roleID) {
        AccessRole ar = entityManager.find(AccessRole.class, roleID);

        if (ar == null) {
            return null;
        }

        return ar.getAccess();
    }

    @Override
    public boolean setRoleAccess(long roleID, Set<String> access) {
        AccessRole ar = entityManager.find(AccessRole.class, roleID);

        if (ar == null) {
            return false;
        }

        ar.setAccess(access);
        entityManager.persist(ar);
        entityManager.flush();
        return true;
    }

    @Override
    public boolean assignRole(long staffAccounID, Set<Long> RoleID) {
        StaffAccount sa = entityManager.find(StaffAccount.class, staffAccounID);

        if (sa == null) {
            return false;
        }

        Set<AccessRole> roles = new HashSet();
        for (Long l : RoleID) {
            AccessRole ar = entityManager.find(AccessRole.class, l);
            roles.add(ar);
        }

        sa.setAccessRole(roles);

        entityManager.persist(sa);
        entityManager.flush();

        return true;
    }

    @Override
    public AccessRole getRoleByName(String roleName) {
        Query query = entityManager.createQuery("SELECT a FROM AccessRole a WHERE a.roleName = :roleName");
        query.setParameter("roleName", roleName);
        List<AccessRole> resultList = new ArrayList();
        resultList = query.getResultList();
        if (resultList.size()>0) {
            return resultList.get(0);
        }
        else return null;
    }

    @Override
    public void removeRole(String roleName) {
        AccessRole ar = getRoleByName(roleName);
        entityManager.remove(ar);
        entityManager.flush();
    }
}
